﻿using JinfuyuanProject.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Repository
{
    public class HomeRepository:IHomeRepository
    {
        private readonly JinfuyuanDbContext _db;
        public HomeRepository(JinfuyuanDbContext db)
        {
            _db = db;
        }
        #region 基础方法
        public bool Delete(Home home)
        {
            _db.Entry(home).State = System.Data.Entity.EntityState.Deleted;
            return _db.SaveChanges() > 0;
        }

        public IQueryable<Home> GetList()
        {
            return _db.Home;
        }

        public Home GetModel(int Id)
        {
            Home home = _db.Home.Find(Id);
            return home;
        }

        public int Insert(Home home)
        {
            _db.Entry(home).State = System.Data.Entity.EntityState.Added;
            _db.SaveChanges();
            return home.Id;
        }

        public bool Update(Home home)
        {
            _db.Entry(home).State = System.Data.Entity.EntityState.Modified;
            return _db.SaveChanges() > 0;
        }
        public IEnumerable<T> QueryBySql<T>(string sql)
        {
            try
            {
                return _db.Database.SqlQuery<T>(sql);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region 业务方法
//        public List<HomeVM> GetRoomDetail()
//        {
//            var homeRooms = QueryBySql<HomeVM>(@"
//select h.*, p.PackageName, c.LiveTime, pt.ParturientName
//from Home h 
//left join Package p on h.PackageId = p.Id
//left join Contract c on h.Id = c.HomeId
//left join Parturient pt on h.Id = pt.HomeId").ToList();
//            return homeRooms;
//        }
        //public ParturientVM GetParturientVM(int id)
        //{
        //    var parturientvm = QueryBySql<ParturientVM>($@"select p.*,c.LiveTime,c.Id as ContractId,b.BirthType 
        //                     from Parturient p
        //                     left join Contract c on p.Id = c.ParturientId
        //                     left join  Home h on c.HomeId = h.Id
        //                     left join  Baby b ON p.Id = b.ParturientId
        //                     where p.HomeId={id}").ToList().FirstOrDefault();
        //    return parturientvm;
        //}
        public List<Baby> GetBabyDetail(int id)
        {
            return QueryBySql<Baby>($@"select b.*
                             from Baby b
                             left join Parturient p on b.ParturientId = p.Id
                             left join Home h ON p.HomeId = h.Id
                             where h.Id={id}").ToList();
        }
        public List<ServiceDoVM> GetServiceDoVM(int ParturientId)
        {
            var time = DateTime.Now;
            return QueryBySql<ServiceDoVM>($@"select  p.Id as ParturientId,s.ServiceName,CASE 
                WHEN s.ServiceType = 1 THEN '宝宝服务'
                WHEN s.ServiceType = 2 THEN '美容服务'
                WHEN s.ServiceType = 3 THEN '护理服务'
                ELSE '其他服务'
                END AS ServiceType,s.Id as ServiceId,sd.ActualTime,sd.FeedBack,sd.ServiceTime,sd.Id
from Parturient p
inner join Contract c on p.Id=c.ParturientId
inner join PackageServices ps on p.PackageId=ps.ComboId
inner join Services s on ps.ServiceId=s.Id
left join ServiceDo sd on c.Id=sd.Cid and s.Id=sd.Sid
where p.Id={ParturientId}").ToList();
        }
        public IQueryable<Product> ProductAPI()
        {
            return _db.Product;
        }
        #endregion
        #region 异步方法
        // 获取Home对象
        public async Task<Home> GetModelAsync(int id)
        {
            return await _db.Set<Home>().FindAsync(id); // 异步查询指定ID的Home记录
        }

        // 更新Home对象
        public async Task<bool> UpdateAsync(Home home)
        {
            try
            {
                _db.Entry(home).State = System.Data.Entity.EntityState.Modified; // 设置状态为修改
                await _db.SaveChangesAsync(); // 异步保存更改
                return true; // 返回成功
            }
            catch (Exception ex)
            {
                // 处理异常（例如记录日志）
                return false; // 返回失败
            }
        }
        #endregion
    }
}
